module Tuscan
  module Iec60584
    module TypeS
      T90_RANGE = -50.00..1768.10
      EMF_RANGE = -0.236..18.693

      extend self

      def emfr_unbound t90
        if t90 < 1064.18
          Polynomial.new(
             0.000000000000e+00,  0.540313308631e-02,  0.125934289740e-04, -0.232477968689e-07,
             0.322028823036e-10, -0.331465196389e-13,  0.255744251786e-16, -0.125068871393e-19,
             0.271443176145e-23
          ).solve_for t90
        elsif t90 < 1664.50
          Polynomial.new(
             0.132900444085e+01,  0.334509311344e-02,  0.654805192818e-05, -0.164856259209e-08,
             0.129989605174e-13
          ).solve_for t90
        else
          Polynomial.new(
             0.146628232636e+03, -0.258430516752e+00,  0.163693574641e-03, -0.330439046987e-07,
            -0.943223690612e-14
          ).solve_for t90
        end
      end

      def t90r_guess emf
        if emf < 1.874
          Polynomial.new(
             0.00000000e+00,  1.84949460e+02, -8.00504062e+01,  1.02237430e+02, -1.52248592e+02,
             1.88821343e+02, -1.59085941e+02,  8.23027880e+01, -2.34181944e+01,  2.79786260e+00
          ).solve_for emf
        elsif emf < 11.950
          Polynomial.new(
             1.291507177e+01,  1.466298863e+02, -1.534713402e+01,  3.145945973e+00, -4.163257839e-01,
             3.187963771e-02, -1.291637500e-03,  2.183475087e-05, -1.447379511e-07,  8.211272125e-09
          ).solve_for emf
        elsif emf < 17.536
          Polynomial.new(
            -8.087801117e+01,  1.621573104e+02, -8.536869453e+00,  4.719686976e-01, -1.441693666e-02,
             2.081618890e-04
          ).solve_for emf
        else
          Polynomial.new(
             5.333875126e+04, -1.235892298e+04,  1.092657613e+03, -4.265693686e+01,  6.247205420e-01
          ).solve_for emf
        end
      end
    end
  end
end

